package cn.iocoder.yudao.module.hs.controller.admin.receiveorder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorder.vo.ReceiveOrderPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorder.vo.ReceiveOrderRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorder.vo.ReceiveOrderSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorder.vo.ReceiveOrderTrayRespVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.receiveorder.ReceiveOrderDO;
import cn.iocoder.yudao.module.hs.service.receiveorder.ReceiveOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收货单")
@RestController
@RequestMapping("/hs/receive-order")
@Validated
public class ReceiveOrderController {

    @Resource
    private ReceiveOrderService receiveOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建收货单")
    @PreAuthorize("@ss.hasPermission('hs:receive-order:create')")
    public CommonResult<Boolean> createReceiveOrder(@Valid @RequestBody ReceiveOrderSaveReqVO createReqVO) {
        receiveOrderService.createReceiveOrder(createReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收货单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:receive-order:delete')")
    public CommonResult<Boolean> deleteReceiveOrder(@RequestParam("id") Long id) {
        receiveOrderService.deleteReceiveOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收货单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:receive-order:query')")
    public CommonResult<ReceiveOrderRespVO> getReceiveOrder(@RequestParam("id") Long id) {
        ReceiveOrderDO receiveOrder = receiveOrderService.getReceiveOrder(id);
        return success(BeanUtils.toBean(receiveOrder, ReceiveOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收货单分页")
    @PreAuthorize("@ss.hasPermission('hs:receive-order:query')")
    public CommonResult<PageResult<ReceiveOrderRespVO>> getReceiveOrderPage(@Valid ReceiveOrderPageReqVO pageReqVO) {
        PageResult<ReceiveOrderDO> pageResult = receiveOrderService.getReceiveOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReceiveOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收货单 Excel")
    @PreAuthorize("@ss.hasPermission('hs:receive-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReceiveOrderExcel(@Valid ReceiveOrderPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiveOrderDO> list = receiveOrderService.getReceiveOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收货单.xls", "数据", ReceiveOrderRespVO.class,
                BeanUtils.toBean(list, ReceiveOrderRespVO.class));
    }

    @GetMapping("/get-tray-list")
    @Operation(summary = "获得收货单中托盘列表")
    @Parameter(name = "shipOrderId", description = "收货单对应的出库单ID", required = true)
    @Parameter(name = "warehouseId", description = "仓库ID", required = true)
    @PreAuthorize("@ss.hasPermission('hs:receive-order:query')")
    public CommonResult<List<ReceiveOrderTrayRespVO>> getTrayList(@RequestParam("shipOrderId") Long shipOrderId, @RequestParam("warehouseId") Long warehouseId) {
        return success(receiveOrderService.getTrayList(shipOrderId, warehouseId));
    }
}